package cn.itsource.basic.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.BaseService;
import cn.itsource.basic.util.PageList;
import cn.itsource.mapper.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    @Transactional
    public void batchDel(List<Long> ids) {
        mapper.batchDel(ids);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 去除 keyword的前后空格
        if (!StringUtils.isEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        // 查询数量
        Integer count = mapper.queryByCount(query);
        // 查询数据
        List<T> ts = mapper.queryByPage(query);
        // 代码的健壮性不够 剩下的东西你们自己补 你们要 判断是否为空！

        return new PageList<T>(ts, count);
    }
}
